#ifndef     __DRAW_TASK_H
#define     __DRAW_TASK_H

#include "../../rtos/thread.h"
#include "../../utils/object.h"
#include "../../rtos/sync_value.h"

namespace task {

class DrawTask : public rtos::Thread<DrawTask, 1024, osPriorityHigh1> {

public:
    enum class State : uint8_t {
        IDLE = 0,
        WORK = 1,
        STOP = 2,
        OPEN = 3,
        TIMEOUT = 4,
    };
    static constexpr uint32_t DRAW_TIMEOUT = 60 * 1000;

    void run();

    inline void draw_auto() { set(State::IDLE); }
    inline void draw_stop() { set(State::STOP); }
    inline void draw_open() { set(State::OPEN); }

    inline uint8_t state() { return static_cast<uint8_t>(m_state); }

private:
    void set(State state) { m_sync.set(state); }
    bool wait(uint32_t timeout = osWaitForever) { return m_sync.get(m_state, timeout); }

    void do_idle();
    void do_work();
    void do_stop();
    void do_open();

    State m_state = State::IDLE;
    rtos::SyncValue<State> m_sync;
};

inline utils::Object<DrawTask> drawTask;

}

#endif
